package com.hymjweb.demo.hymj.flight_manage.mapper;

import com.hymjweb.demo.framework.cache.bean.AMyCacheTreeNode;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.flight_manage.bean.BFlightPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface BFlightMapper {

    class BFlightMapperProvider {

        public String findAll(Map map) {

            String sql = new SQL() {{
                SELECT("t.*");
                FROM("b_flight t where 1 = 1 ");
            }}.toString();

            if (map != null) {

                String keyFlightStartDate = DataTypeUtil.toStringObject(map.get("keyFlightStartDate"));
                String keyFlightEndDate = DataTypeUtil.toStringObject(map.get("keyFlightEndDate"));
                String keyCityId = DataTypeUtil.toStringObject(map.get("keyCityId"));
                String keyAirportId = DataTypeUtil.toStringObject(map.get("keyAirportId"));
                String keyAirlinesId = DataTypeUtil.toStringObject(map.get("keyAirlinesId"));
                String keyFlightNo = DataTypeUtil.toStringObject(map.get("keyFlightNo"));


                if (keyFlightStartDate != null) {
                    if (keyFlightEndDate != null) {
                        int d = keyFlightStartDate.compareTo(keyFlightEndDate);
                        if (d > 0) {
                            String tempDate = keyFlightStartDate;
                            keyFlightStartDate = keyFlightEndDate;
                            keyFlightEndDate = tempDate;
                        }
                        // and (date_format(t.create_time,'%Y-%m-%d') between keyOpenBillStartDate and keyOpenBillEndDate)
                        sql += " and (date_format(t.flight_date,'%Y-%m-%d') between " + DataTypeUtil.quoteStr(keyFlightStartDate) + " and " + DataTypeUtil.quoteStr(keyFlightEndDate) + ")";
                    } else {
                        sql += " and date_format(t.flight_date,'%Y-%m-%d') = " + DataTypeUtil.quoteStr(keyFlightStartDate);
                    }
                } else {
                    if (keyFlightEndDate != null) {
                        sql += " and date_format(t.flight_date,'%Y-%m-%d') = " + DataTypeUtil.quoteStr(keyFlightEndDate);
                    }
                }

                if (!DataTypeUtil.isEmptyStr(keyCityId)) {
                    sql += " and t.city_id = " + DataTypeUtil.quoteStr(keyCityId);
                }


                if (!DataTypeUtil.isEmptyStr(keyAirportId)) {
                    sql += " and t.airport_id = " + DataTypeUtil.quoteStr(keyAirportId);
                }


                if (!DataTypeUtil.isEmptyStr(keyAirlinesId)) {
                    sql += " and t.airlines_id = " + DataTypeUtil.quoteStr(keyAirlinesId);
                }

                if (!DataTypeUtil.isEmptyStr(keyFlightNo)) {
                    sql += " and t.flight_no = " + DataTypeUtil.quoteStr(keyFlightNo);
                }

            }
            return sql;
        }

        public String insert(BFlightPojo pojo) {
            String sql = new SQL() {{
                INSERT_INTO("b_flight");
                VALUES("id", "#{id}");
                VALUES("city_id", "#{cityId}");
                VALUES("airport_id", "#{airportId}");
                VALUES("airlines_id", "#{airlinesId}");
                VALUES("flight_no", "#{flightNo}");
                VALUES("flight_date", "#{flightDate}");
                VALUES("plan_take_off_time", "#{planTakeOffTime}");
                VALUES("model", "#{model}");
                VALUES("comment", "#{comment}");
                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("update_time", "#{updateTime}");
                VALUES("update_user_id", "#{updateUserId}");
            }}.toString();
            return sql;
        }

        public String update(BFlightPojo pojo) {
            String sql = new SQL() {{
                UPDATE("b_flight t");
                SET("t.city_id = #{cityId}");
                SET("t.airport_id = #{airportId}");
                SET("t.airlines_id = #{airlinesId}");
                SET("t.flight_no = #{flightNo}");
                SET("t.flight_date = #{flightDate}");
                SET("t.plan_take_off_time = #{planTakeOffTime}");
                SET("t.model = #{model}");
                SET("t.comment = #{comment}");
                SET("t.update_time = #{updateTime}");
                SET("t.update_user_id = #{updateUserId}");
                WHERE("t.id= #{id}");
            }}.toString();
            return sql;
        }

        public String delete(String id) {
            String sql = new SQL() {{
                DELETE_FROM("b_flight");
                WHERE("id = " + id);
            }}.toString();
            return sql;
        }

        public String findObjectById(String id) {
            return new SQL() {{
                SELECT("t.*");
                FROM("b_flight t");
                WHERE("t.id = " + id);
            }}.toString();
        }

        //UK (city_id, airport_id, airlines_id, flight_no, flight_date)
        public String findUK(BFlightPojo pojo) {
            String sql = new SQL() {{
                SELECT("t.* ");
                FROM("b_flight t ");
                WHERE(" t.city_id = #{cityId}");
                WHERE(" t.airport_id = #{airportId}");
                WHERE(" t.airlines_id = #{airlinesId}");
                WHERE(" t.flight_no = #{flightNo}");
                WHERE(" date_format(t.flight_date,'%Y-%m-%d')  = " + DataTypeUtil.quoteStr(DataTypeUtil.toNotNullString(pojo.getFlightDate())));
            }}.toString();
            return sql;
        }

        public String findByFlightDate(String flightDate) {
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("b_flight t ");
                WHERE("1=1 and date_format(t.flight_date,'%Y-%m-%d') = #{flightDate}");
            }}.toString();
            return sql;
        }

        public String findFlightNo(String airportId, String cityId, String airlinesId, String flightDate) {
            String sql = new SQL() {{
                SELECT("distinct t.flight_no id,t.flight_no name");
                FROM("b_flight t");
                WHERE(" t.airport_id = " + DataTypeUtil.quoteStr(airportId));
                WHERE(" t.city_id = " + DataTypeUtil.quoteStr(cityId));
                WHERE(" t.airlines_id = " + DataTypeUtil.quoteStr(airlinesId));
            }}.toString();
            return sql;
        }

        public String queryFlightNoPojoById(String flightId) {
            String sql = new SQL() {{
                SELECT("*");
                FROM("b_flight t");
                WHERE(" t.id = #{flightId}");
            }}.toString();
            return sql;
        }


        public String queryFlightNoListByDropCnd(Map map) {
            String sql = new SQL() {{
                SELECT("t.id, xal.short_name, t.flight_no AS NAME, t.plan_take_off_time, xal.id airlines_id ");
                FROM("b_flight t, xt_city xc, xt_airport xta, xt_airlines xal ");
                WHERE(" 1 = 1 AND t.airlines_id = xal.ID AND t.city_id = xc.ID AND t.airport_id = xta.id ");
            }}.toString();

            String cityId = DataTypeUtil.toNotNullString(map.get("cityId"));
            String airlinesId = DataTypeUtil.toNotNullString(map.get("airlinesId"));
            String flightDate = DataTypeUtil.toNotNullString(map.get("flightDate"));
            String airportId = DataTypeUtil.toNotNullString(map.get("airportId"));

            if (!DataTypeUtil.isEmptyStr(cityId)) {
                sql += " AND t.city_id = " + DataTypeUtil.quoteStr(cityId);
            }

            if (!DataTypeUtil.isEmptyStr(airlinesId)) {
                sql += " AND t.airlines_id = " + DataTypeUtil.quoteStr(airlinesId);
            }

            if (!DataTypeUtil.isEmptyStr(airportId)) {
                sql += " AND t.airport_id = " + DataTypeUtil.quoteStr(airportId);
            }

            if (!DataTypeUtil.isEmptyStr(flightDate)) {
                sql += " AND DATE_FORMAT(t.flight_date, '%Y-%m-%d' ) = " + DataTypeUtil.quoteStr(flightDate);
            }

            return sql;
        }
    }

    @Results(id = "bFlightMapId", value = {
            @Result(column = "id", property = "id"),
            @Result(column = "city_id", property = "cityId"),
            @Result(column = "airport_id", property = "airportId"),
            @Result(column = "airlines_id", property = "airlinesId"),
            @Result(column = "flight_no", property = "flightNo"),
            @Result(column = "flight_date", property = "flightDate"),
            @Result(column = "plan_take_off_time", property = "planTakeOffTime"),
            @Result(column = "model", property = "model"),
            @Result(column = "comment", property = "comment"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "create_user_id", property = "createUserId"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "update_user_id", property = "updateUserId")
    })
    @SelectProvider(type = BFlightMapperProvider.class, method = "findAll")
    List<BFlightPojo> findAll(Map map);

    @ResultMap(value = "bFlightMapId")
    @SelectProvider(type = BFlightMapperProvider.class, method = "findObjectById")
    BFlightPojo findObjectById(@Param("id") String id);

    @ResultMap(value = "bFlightMapId")
    @SelectProvider(type = BFlightMapperProvider.class, method = "queryFlightNoPojoById")
    BFlightPojo queryFlightNoPojoById(String flightId);

    @ResultMap("bFlightMapId")
    @SelectProvider(type = BFlightMapperProvider.class, method = "findUK")
    BFlightPojo findUK(BFlightPojo pojo);

    @ResultMap(value = "bFlightMapId")
    @SelectProvider(type = BFlightMapperProvider.class, method = "findByFlightDate")
    List<BFlightPojo> findByFlightDate(@Param("flightDate") String flightDate);

    @ResultType(Map.class)
    @MapKey("id")
    @SelectProvider(type = BFlightMapperProvider.class, method = "queryFlightNoListByDropCnd")
    List<Map<String, Object>> queryFlightNoListByDropCnd(Map param);


    @Results(id = "findFlightNo", value = {
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
    })
    @SelectProvider(type = BFlightMapperProvider.class, method = "findFlightNo")
    List<AMyCacheTreeNode> findFlightNo(String airportId, String cityId, String airlinesId, String flightDate);


    @InsertProvider(type = BFlightMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(BFlightPojo obj);

    @Insert(
            "<script>" +
                    "insert into b_flight(id, city_id, airport_id, airlines_id, flight_no, flight_date, plan_take_off_time, model, comment, create_time, create_user_id, update_time, update_user_id) values " +
                    "<foreach collection='bFlightPojos' item='pojo' separator=','>" +
                    "(#{pojo.id},#{pojo.cityId},#{pojo.airportId},#{pojo.airlinesId},#{pojo.flightNo},#{pojo.flightDate},#{pojo.planTakeOffTime},#{pojo.model},#{pojo.comment},#{pojo.createTime},#{pojo.createUserId},#{pojo.updateTime},#{pojo.updateUserId})" +
                    "</foreach>" +
                    "</script>"
    )
    @Options(keyProperty = "id")
    int batchInsert(@Param("bFlightPojos") List<BFlightPojo> pojos);

    @UpdateProvider(type = BFlightMapperProvider.class, method = "update")
    int update(BFlightPojo obj);

    @DeleteProvider(type = BFlightMapperProvider.class, method = "delete")
    int delete(String id);


}
